home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 21 / AACD 21.iso / AACD / Utilities / Ghostscript / src / zarith.c < prev    next >
Encoding:
C/C++ Source or Header  |  2001-01-01  |  8.8 KB  |  416 lines

  1. /* Copyright (C) 1989, 2000 Aladdin Enterprises.  All rights reserved.
  2.   
  3.   This file is part of AFPL Ghostscript.
  4.   
  5.   AFPL Ghostscript is distributed with NO WARRANTY OF ANY KIND.  No author or
  6.   distributor accepts any responsibility for the consequences of using it, or
  7.   for whether it serves any particular purpose or works at all, unless he or
  8.   she says so in writing.  Refer to the Aladdin Free Public License (the
  9.   "License") for full details.
  10.   
  11.   Every copy of AFPL Ghostscript must include a copy of the License, normally
  12.   in a plain ASCII text file named PUBLIC.  The License grants you the right
  13.   to copy, modify and redistribute AFPL Ghostscript, but only under certain
  14.   conditions described in the License.  Among other things, the License
  15.   requires that the copyright notice and this notice be preserved on all
  16.   copies.
  17. */
  18.  
  19. /*$Id: zarith.c,v 1.4 2000/09/19 19:00:52 lpd Exp $ */
  20. /* Arithmetic operators */
  21. #include "math_.h"
  22. #include "ghost.h"
  23. #include "oper.h"
  24. #include "store.h"
  25.  
  26. /****** NOTE: none of the arithmetic operators  ******/
  27. /****** currently check for floating exceptions ******/
  28.  
  29. /*
  30.  * Many of the procedures in this file are public only so they can be
  31.  * called from the FunctionType 4 interpreter (zfunc4.c).
  32.  */
  33.  
  34. /* Define max and min values for what will fit in value.intval. */
  35. #define MIN_INTVAL min_long
  36. #define MAX_INTVAL max_long
  37. #define MAX_HALF_INTVAL ((1L << (size_of(long) * 4 - 1)) - 1)
  38.  
  39. /* <num1> <num2> add <sum> */
  40. /* We make this into a separate procedure because */
  41. /* the interpreter will almost always call it directly. */
  42. int
  43. zop_add(register os_ptr op)
  44. {
  45.     switch (r_type(op)) {
  46.     default:
  47.     return_op_typecheck(op);
  48.     case t_real:
  49.     switch (r_type(op - 1)) {
  50.     default:
  51.         return_op_typecheck(op - 1);
  52.     case t_real:
  53.         op[-1].value.realval += op->value.realval;
  54.         break;
  55.     case t_integer:
  56.         make_real(op - 1, (double)op[-1].value.intval + op->value.realval);
  57.     }
  58.     break;
  59.     case t_integer:
  60.     switch (r_type(op - 1)) {
  61.     default:
  62.         return_op_typecheck(op - 1);
  63.     case t_real:
  64.         op[-1].value.realval += (double)op->value.intval;
  65.         break;
  66.     case t_integer: {
  67.         long int2 = op->value.intval;
  68.  
  69.         if (((op[-1].value.intval += int2) ^ int2) < 0 &&
  70.         ((op[-1].value.intval - int2) ^ int2) >= 0
  71.         ) {            /* Overflow, convert to real */
  72.         make_real(op - 1, (double)(op[-1].value.intval - int2) + int2);
  73.         }
  74.     }
  75.     }
  76.     }
  77.     return 0;
  78. }
  79. int
  80. zadd(i_ctx_t *i_ctx_p)
  81. {
  82.     os_ptr op = osp;
  83.     int code = zop_add(op);
  84.  
  85.     if (code == 0) {
  86.     pop(1);
  87.     }
  88.     return code;
  89. }
  90.  
  91. /* <num1> <num2> div <real_quotient> */
  92. int
  93. zdiv(i_ctx_t *i_ctx_p)
  94. {
  95.     os_ptr op = osp;
  96.     os_ptr op1 = op - 1;
  97.  
  98.     /* We can't use the non_int_cases macro, */
  99.     /* because we have to check explicitly for op == 0. */
  100.     switch (r_type(op)) {
  101.     default:
  102.         return_op_typecheck(op);
  103.     case t_real:
  104.         if (op->value.realval == 0)
  105.         return_error(e_undefinedresult);
  106.         switch (r_type(op1)) {
  107.         default:
  108.             return_op_typecheck(op1);
  109.         case t_real:
  110.             op1->value.realval /= op->value.realval;
  111.             break;
  112.         case t_integer:
  113.             make_real(op1, (double)op1->value.intval / op->value.realval);
  114.         }
  115.         break;
  116.     case t_integer:
  117.         if (op->value.intval == 0)
  118.         return_error(e_undefinedresult);
  119.         switch (r_type(op1)) {
  120.         default:
  121.             return_op_typecheck(op1);
  122.         case t_real:
  123.             op1->value.realval /= (double)op->value.intval;
  124.             break;
  125.         case t_integer:
  126.             make_real(op1, (double)op1->value.intval / (double)op->value.intval);
  127.         }
  128.     }
  129.     pop(1);
  130.     return 0;
  131. }
  132.  
  133. /* <num1> <num2> mul <product> */
  134. int
  135. zmul(i_ctx_t *i_ctx_p)
  136. {
  137.     os_ptr op = osp;
  138.  
  139.     switch (r_type(op)) {
  140.     default:
  141.     return_op_typecheck(op);
  142.     case t_real:
  143.     switch (r_type(op - 1)) {
  144.     default:
  145.         return_op_typecheck(op - 1);
  146.     case t_real:
  147.         op[-1].value.realval *= op->value.realval;
  148.         break;
  149.     case t_integer:
  150.         make_real(op - 1, (double)op[-1].value.intval * op->value.realval);
  151.     }
  152.     break;
  153.     case t_integer:
  154.     switch (r_type(op - 1)) {
  155.     default:
  156.         return_op_typecheck(op - 1);
  157.     case t_real:
  158.         op[-1].value.realval *= (double)op->value.intval;
  159.         break;
  160.     case t_integer: {
  161.         long int1 = op[-1].value.intval;
  162.         long int2 = op->value.intval;
  163.         long abs1 = (int1 >= 0 ? int1 : -int1);
  164.         long abs2 = (int2 >= 0 ? int2 : -int2);
  165.         float fprod;
  166.  
  167.         if ((abs1 > MAX_HALF_INTVAL || abs2 > MAX_HALF_INTVAL) &&
  168.         /* At least one of the operands is very large. */
  169.         /* Check for integer overflow. */
  170.         abs1 != 0 &&
  171.         abs2 > MAX_INTVAL / abs1 &&
  172.         /* Check for the boundary case */
  173.         (fprod = (float)int1 * int2,
  174.          (int1 * int2 != MIN_INTVAL ||
  175.           fprod != (float)MIN_INTVAL))
  176.         )
  177.         make_real(op - 1, fprod);
  178.         else
  179.         op[-1].value.intval = int1 * int2;
  180.     }
  181.     }
  182.     }
  183.     pop(1);
  184.     return 0;
  185. }
  186.  
  187. /* <num1> <num2> sub <difference> */
  188. /* We make this into a separate procedure because */
  189. /* the interpreter will almost always call it directly. */
  190. int
  191. zop_sub(register os_ptr op)
  192. {
  193.     switch (r_type(op)) {
  194.     default:
  195.     return_op_typecheck(op);
  196.     case t_real:
  197.     switch (r_type(op - 1)) {
  198.     default:
  199.         return_op_typecheck(op - 1);
  200.     case t_real:
  201.         op[-1].value.realval -= op->value.realval;
  202.         break;
  203.     case t_integer:
  204.         make_real(op - 1, (double)op[-1].value.intval - op->value.realval);
  205.     }
  206.     break;
  207.     case t_integer:
  208.     switch (r_type(op - 1)) {
  209.     default:
  210.         return_op_typecheck(op - 1);
  211.     case t_real:
  212.         op[-1].value.realval -= (double)op->value.intval;
  213.         break;
  214.     case t_integer: {
  215.         long int1 = op[-1].value.intval;
  216.  
  217.         if ((int1 ^ (op[-1].value.intval = int1 - op->value.intval)) < 0 &&
  218.         (int1 ^ op->value.intval) < 0
  219.         ) {            /* Overflow, convert to real */
  220.         make_real(op - 1, (float)int1 - op->value.intval);
  221.         }
  222.     }
  223.     }
  224.     }
  225.     return 0;
  226. }
  227. int
  228. zsub(i_ctx_t *i_ctx_p)
  229. {
  230.     os_ptr op = osp;
  231.     int code = zop_sub(op);
  232.  
  233.     if (code == 0) {
  234.     pop(1);
  235.     }
  236.     return code;
  237. }
  238.  
  239. /* <num1> <num2> idiv <int_quotient> */
  240. int
  241. zidiv(i_ctx_t *i_ctx_p)
  242. {
  243.     os_ptr op = osp;
  244.  
  245.     check_type(*op, t_integer);
  246.     check_type(op[-1], t_integer);
  247.     if (op->value.intval == 0)
  248.     return_error(e_undefinedresult);
  249.     if ((op[-1].value.intval /= op->value.intval) ==
  250.     MIN_INTVAL && op->value.intval == -1
  251.     ) {            /* Anomalous boundary case, fail. */
  252.     return_error(e_rangecheck);
  253.     }
  254.     pop(1);
  255.     return 0;
  256. }
  257.  
  258. /* <int1> <int2> mod <remainder> */
  259. int
  260. zmod(i_ctx_t *i_ctx_p)
  261. {
  262.     os_ptr op = osp;
  263.  
  264.     check_type(*op, t_integer);
  265.     check_type(op[-1], t_integer);
  266.     if (op->value.intval == 0)
  267.     return_error(e_undefinedresult);
  268.     op[-1].value.intval %= op->value.intval;
  269.     pop(1);
  270.     return 0;
  271. }
  272.  
  273. /* <num1> neg <num2> */
  274. int
  275. zneg(i_ctx_t *i_ctx_p)
  276. {
  277.     os_ptr op = osp;
  278.  
  279.     switch (r_type(op)) {
  280.     default:
  281.         return_op_typecheck(op);
  282.     case t_real:
  283.         op->value.realval = -op->value.realval;
  284.         break;
  285.     case t_integer:
  286.         if (op->value.intval == MIN_INTVAL)
  287.         make_real(op, -(float)MIN_INTVAL);
  288.         else
  289.         op->value.intval = -op->value.intval;
  290.     }
  291.     return 0;
  292. }
  293.  
  294. /* <num1> abs <num2> */
  295. int
  296. zabs(i_ctx_t *i_ctx_p)
  297. {
  298.     os_ptr op = osp;
  299.  
  300.     switch (r_type(op)) {
  301.     default:
  302.         return_op_typecheck(op);
  303.     case t_real:
  304.         if (op->value.realval >= 0)
  305.         return 0;
  306.         break;
  307.     case t_integer:
  308.         if (op->value.intval >= 0)
  309.         return 0;
  310.         break;
  311.     }
  312.     return zneg(i_ctx_p);
  313. }
  314.  
  315. /* <num1> ceiling <num2> */
  316. int
  317. zceiling(i_ctx_t *i_ctx_p)
  318. {
  319.     os_ptr op = osp;
  320.  
  321.     switch (r_type(op)) {
  322.     default:
  323.         return_op_typecheck(op);
  324.     case t_real:
  325.         op->value.realval = ceil(op->value.realval);
  326.     case t_integer:;
  327.     }
  328.     return 0;
  329. }
  330.  
  331. /* <num1> floor <num2> */
  332. int
  333. zfloor(i_ctx_t *i_ctx_p)
  334. {
  335.     os_ptr op = osp;
  336.  
  337.     switch (r_type(op)) {
  338.     default:
  339.         return_op_typecheck(op);
  340.     case t_real:
  341.         op->value.realval = floor(op->value.realval);
  342.     case t_integer:;
  343.     }
  344.     return 0;
  345. }
  346.  
  347. /* <num1> round <num2> */
  348. int
  349. zround(i_ctx_t *i_ctx_p)
  350. {
  351.     os_ptr op = osp;
  352.  
  353.     switch (r_type(op)) {
  354.     default:
  355.         return_op_typecheck(op);
  356.     case t_real:
  357.         op->value.realval = floor(op->value.realval + 0.5);
  358.     case t_integer:;
  359.     }
  360.     return 0;
  361. }
  362.  
  363. /* <num1> truncate <num2> */
  364. int
  365. ztruncate(i_ctx_t *i_ctx_p)
  366. {
  367.     os_ptr op = osp;
  368.  
  369.     switch (r_type(op)) {
  370.     default:
  371.         return_op_typecheck(op);
  372.     case t_real:
  373.         op->value.realval =
  374.         (op->value.realval < 0.0 ?
  375.          ceil(op->value.realval) :
  376.          floor(op->value.realval));
  377.     case t_integer:;
  378.     }
  379.     return 0;
  380. }
  381.  
  382. /* Non-standard operators */
  383.  
  384. /* <int1> <int2> .bitadd <sum> */
  385. private int
  386. zbitadd(i_ctx_t *i_ctx_p)
  387. {
  388.     os_ptr op = osp;
  389.  
  390.     check_type(*op, t_integer);
  391.     check_type(op[-1], t_integer);
  392.     op[-1].value.intval += op->value.intval;
  393.     pop(1);
  394.     return 0;
  395. }
  396.  
  397. /* ------ Initialization table ------ */
  398.  
  399. const op_def zarith_op_defs[] =
  400. {
  401.     {"1abs", zabs},
  402.     {"2add", zadd},
  403.     {"2.bitadd", zbitadd},
  404.     {"1ceiling", zceiling},
  405.     {"2div", zdiv},
  406.     {"2idiv", zidiv},
  407.     {"1floor", zfloor},
  408.     {"2mod", zmod},
  409.     {"2mul", zmul},
  410.     {"1neg", zneg},
  411.     {"1round", zround},
  412.     {"2sub", zsub},
  413.     {"1truncate", ztruncate},
  414.     op_def_end(0)
  415. };
  416.